package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.model.GroupModel;
import com.hangzhou.xyj.model.GroupPageInfoModel;
import com.hangzhou.xyj.net.handler.QueryGroupListHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.QueryGroupListReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.MoreGroupAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.utils.constants.Constants;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.List;

/**
 * Created by neo.duan on 2016/1/19.
 * 更多圈子
 */
public class MoreGroupActivity extends BaseActivity {

    public static final int INTENT_TAG_NEW = 1;
    public static final int INTENT_TAG_FAMOUS = 2;
    public static final int INTENT_TAG_RECOMMEND = 3;
    public static final int INTENT_TAG_NEAR = 4;

    private RecyclerView mRecyclerView;
    private int mIntentTag;
    private MoreGroupAdapter mAdapter;

    @Override
    public void initTop() {
        String title = "";
        switch (mIntentTag) {
            case INTENT_TAG_NEW:
                title = "新兴圈子";
                break;
            case INTENT_TAG_FAMOUS:
                title = "演员部落";
                break;
            case INTENT_TAG_RECOMMEND:
                title = "推荐圈子";
                break;
            case INTENT_TAG_NEAR:
                title = "附近圈子";
                break;
        }
        enableTitle(true, title);
        enableRefresh(true);
    }

    @Override
    public void initIntentData(Intent intent) {
        super.initIntentData(intent);
        mIntentTag = intent.getIntExtra(DataKey.INTENT_TAG, -1);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.layout_common_recycleview);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_common);
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        //设置水平分割线
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
//                , R.drawable.line_divider_one));
        mRecyclerView.setAdapter(mAdapter = new MoreGroupAdapter(mRecyclerView, null));
    }

    @Override
    public void initDatas() {
        getData(1);
    }

    @Override
    public void initListeners() {
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getData(mAdapter.getCurrentPage());
                    }
                }, 800);

            }
        });
    }


    public void getData(final int currentPage) {
        QueryGroupListReq queryGroupListReq;
        switch (mIntentTag) {
            case INTENT_TAG_NEW://新兴部落
                queryGroupListReq = new QueryGroupListReq(null, null, null, QueryGroupListReq.TYPE_NEW, Constants.PAGE_SIZE, currentPage);
                break;
            case INTENT_TAG_NEAR://附近圈子
                queryGroupListReq = new QueryGroupListReq(null, XYJApplication.getCity(), null, null, Constants.PAGE_SIZE, currentPage);
                break;
            case INTENT_TAG_RECOMMEND://推荐圈子
                queryGroupListReq = new QueryGroupListReq(null, null, "recommend", null, Constants.PAGE_SIZE, currentPage);
                break;
            case INTENT_TAG_FAMOUS: //名家部落
            default:
                queryGroupListReq = new QueryGroupListReq(null, null, null, QueryGroupListReq.TYPE_EXPERT, Constants.PAGE_SIZE, currentPage);
                break;
        }
        HttpLoader.getInstance().sendRequest(new QueryGroupListHandler(queryGroupListReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<GroupModel> groupList = ((GroupPageInfoModel) model).getGroupList();
                if (currentPage == 1 && (groupList == null || groupList.isEmpty())) {
                    setNoDataTip(true, "暂时没有数据哦");
                } else if (currentPage == 1) {
                    mAdapter.load(groupList);
                } else {
                    mAdapter.update(groupList);
                }
            }
            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }
}
